<template>
  <div class="loginBody">
    <div class="loginDiv">
      <div class="login-content">
        <h1 class="login-title">用户登录</h1>
        <el-form center :model="loginForm" label-width="100px" :rules="rules" ref="loginForm">
          <el-form-item label="账号" prop="account"
            ><el-input
              type="text"
              style="width: 200px"
              v-model="loginForm.account"
              autocomplete="off"
            ></el-input>
          </el-form-item>
          <el-form-item label="密码" prop="password"
            ><el-input
              type="password"
              v-model="loginForm.password"
              style="width: 200px"
              show-password
              autocomplete="off"
              @keyup.enter="confirm"
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-button
              style="margin-left: 60px"
              type="primary"
              @click="confirm"
              :disabled="confirm_disabled"
              >登录</el-button
            >
          </el-form-item>
          <div class="unlogin" style="text-align: center">
            <router-link :to="{ path: '/forgetpwd' }"> 忘记密码? </router-link>
            |
            <router-link :to="{ path: '/register' }">
              <a href="register.vue" target="_blank" align="right">注册新账号</a>
            </router-link>
          </div>
        </el-form>
      </div>
    </div>
  </div>
</template>
<script scoped>
export default {
  name: 'Login',
  data() {
    return {
      confirm_disabled: false,
      loginForm: {
        account: '',
        password: '',
      },
      rules: {
        account: [{ required: true, message: '请输入账号', trigger: 'blur' }],
        password: [{ required: true, message: '请输入密码', trigger: 'blur' }],
      },
    };
  },
  methods: {
    confirm() {
      this.confirm_disabled = true;
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          this.$axios
            .post(this.$httpUrl + '/user/login', this.loginForm)
            .then((res) => res.data)
            .then((res) => {
              if (res.code == 200) {
                sessionStorage.setItem('User', JSON.stringify(res.data.user));
                console.log(JSON.stringify(res.data.user));
                this.$store.dispatch('updateMenu', res.data.menu);
                console.log('login');
                console.log(JSON.stringify(res.data.menu));
                this.$router.replace('/Index');
              } else {
                this.confirm_disabled = false;
                alert('用户名或密码错误！');
                return false;
              }
            });
        } else {
          this.confirm_disabled = false;
          console.log('校验失败');
          return false;
        }
      });
    },
  },
};
</script>
<style scoped>
.loginBody {
  position: absolute;
  width: 100%;
  height: 100%;
  background-color: #b3c0d1;
}
.loginDiv {
  position: absolute;
  top: 50%;
  left: 50%;
  margin-top: -200px;
  margin-left: -250px;
  width: 450px;
  height: 330px;
  background: #fff;
  border-radius: 5%;
}
.login-title {
  margin: 20px 0;
  text-align: center;
}
.login-content {
  width: 400px;
  height: 250px;
  position: absolute;
  top: 25px;
  left: 25px;
}
</style>
